package com.itheima.web3.service.impl;

import com.itheima.web3.dao.UserMapping;
import com.itheima.web3.pojo.User;
import com.itheima.web3.service.UserService;
import com.itheima.web3.util.SqlSessionUtil;
import org.apache.ibatis.session.SqlSession;

public class UserServiceImpl implements UserService {
    @Override
    public User login(User user) {
        SqlSession sqlSession = SqlSessionUtil.createSqlSession(true);
        UserMapping mapper = sqlSession.getMapper(UserMapping.class);
        User user1 = mapper.login(user);
        sqlSession.close();
        return user1;
    }

    @Override
    public boolean queryUserByUsername(User user) {
        SqlSession sqlSession = SqlSessionUtil.createSqlSession(true);
        UserMapping mapper = sqlSession.getMapper(UserMapping.class);
        User user1 = mapper.queryUserByUsername(user.getUsername());

        sqlSession.close();
        if (user1 != null) {
            return false;
        }
        return true;
    }

    @Override
    public int addUser(User user) {
        SqlSession sqlSession = SqlSessionUtil.createSqlSession(true);
        UserMapping mapper = sqlSession.getMapper(UserMapping.class);
        int count = mapper.addUser(user);
        sqlSession.close();
        return count;
    }
}
